package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.lxj.xpopup.XPopup;
import com.umeng.commonsdk.debug.E;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.KhRecordActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.DealerDetailBean;
import com.weimei.conversionTreasure.http.entity.SaveListBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.weight.BottomAddPop;
import com.weimei.conversionTreasure.weight.BottomAddRecordPop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class SaveXlVM extends BaseViewModel<Repository> {
    public SaveXlVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> type = new ObservableField<>("1");

    public ObservableList<XLItemVM> observableListXL = new ObservableArrayList<>();
    public ItemBinding<XLItemVM> itemBindingXL = ItemBinding.of(BR.viewModel, R.layout.item_xl);

    public ObservableList<CCItemVM> observableListCC = new ObservableArrayList<>();
    public ItemBinding<CCItemVM> itemBindingCC = ItemBinding.of(BR.viewModel, R.layout.item_c);

    public ObservableInt empty = new ObservableInt(View.GONE);
    public ObservableInt ccRecy = new ObservableInt(View.GONE);
    public ObservableInt xlRecy = new ObservableInt(View.VISIBLE);

    public ObservableField<String> title = new ObservableField<>();
    public ObservableField<String> recordArr = new ObservableField<>();

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }

    public BindingCommand add = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomAddPop(mActivity, new BottomAddPop.Click() {
                        @Override
                        public void click(String s, String records, String type) {
                            title.set(s);
                            recordArr.set(records);
                            if (type.equals("0")){
                                addCCData();
                            }else {
                                addXLData();
                            }

                        }
                    }))
                    .show();
        }
    });

    @Override
    public void onResume() {
        super.onResume();
        if (type.get().equals("0")){
            getCCList();
            ccRecy.set(View.VISIBLE);
            xlRecy.set(View.GONE);
        }else {
            getXLList();
            ccRecy.set(View.GONE);
            xlRecy.set(View.VISIBLE);
        }
    }

    private void getXLList(){
        observableListXL.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.blankinglist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SaveListBean>() {
                    @Override
                    public void onNext(SaveListBean detailBean) {
                        if (detailBean.issucc){
                            if (detailBean.getData().size() > 0){
                                empty.set(View.GONE);
                                for (SaveListBean.DataDTO bean:detailBean.getData()){
                                    XLItemVM itemVM = new XLItemVM(SaveXlVM.this,bean);
                                    observableListXL.add(itemVM);
                                }
                            }else {
                                empty.set(View.VISIBLE);
                            }

                        }else {
                            empty.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getCCList(){
        observableListCC.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.storagelist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SaveListBean>() {
                    @Override
                    public void onNext(SaveListBean detailBean) {
                        if (detailBean.issucc){
                            if (detailBean.getData().size() > 0){
                                empty.set(View.GONE);
                                for (SaveListBean.DataDTO bean:detailBean.getData()){
                                    CCItemVM itemVM = new CCItemVM(SaveXlVM.this,bean);
                                    observableListCC.add(itemVM);
                                }
                            }else {
                                empty.set(View.VISIBLE);
                            }

                        }else {
                            empty.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void delXL(int id){
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid", id+"");
        model.deletestorage(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void addXLData(){
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("title", title.get()+"");
        map.put("recordArr", recordArr.get()+"");

        model.addblanking(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void addCCData(){
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("title", title.get()+"");
        map.put("recordArr", recordArr.get()+"");

        model.addstorage(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void showDetail(SaveListBean.DataDTO dataDTO) {
        new XPopup.Builder(mActivity)
                .dismissOnTouchOutside(false)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(false)
                .asCustom(new BottomAddRecordPop(mActivity, (s, records) -> {
                    if (ValidateUtils.isNotEmptyString(s)){
                        title.set(s);
                        recordArr.set(records);
                        if (type.get().equals("0")){
                            addCCData();
                        }else {
                            addXLData();
                        }
                    }
                },dataDTO))
                .show();
    }
}
